package com.mediacloud.app.newsmodule.fragment.navlive

import android.content.Context
import android.graphics.Color
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.utils.DefaultBgUtil
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX

/**
 * 频道列表
 */
class NavLiveChannelTableAdapter(context: Context?) : BaseRecyclerAdapterX<ArticleItem>(context)
{
    var selectedIndex: Int=0
    set(value) {
        field=value
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder
    {
        return ChannelTableHolder(LayoutInflater.from(context).inflate(R.layout.navchanletableitem,parent,false))
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int)
    {
        super.onBindViewHolder(holder, position)
        if(holder is ChannelTableHolder)
        {
            holder.update(selectedIndex==position,getItem(position))
        }
    }

    private class ChannelTableHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView)
    {
        val tableChannelName:TextView=itemView.findViewById(R.id.tableChannelName)
        val table:View=itemView.findViewById(R.id.tab)
        val c1 = ContextCompat.getColor(itemView.context,R.color._222)
        val c2 = ContextCompat.getColor(itemView.context,R.color.cctv7_red_light)

        fun update(isSelected: Boolean, item: ArticleItem)
        {
            if(isSelected){

                tableChannelName.setTextColor(c2)
                table.visibility = View.VISIBLE
            }
            else{
                tableChannelName.setTextColor(c1)
                table.visibility = View.GONE
            }
            tableChannelName.text=item.title
        }
    }
}